﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace Web.Admin.Page_System
{
    public partial class DataBase : Evan.WebUI.ManagerPage
    {
        protected List<Evan.Model.SysModel.FileModel> DbBackList = new List<Evan.Model.SysModel.FileModel>();
        string comurl = "?t=DataBase&action=editDataBase";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                string op = Evan.Common.CommFun.GetQueryString("op");
                switch (op)
                {
                    case "back":
                        Back();
                        break;
                    case "restore":
                        //ReStore();
                        break;
                    case "download":
                        //DownLoad();
                        break;
                    case "delete":
                        //Delete();
                        break;
                    default:
                        GetBakList();
                        break;
                }
                GetBakList();
            }

        }

        public void Back()
        {
            bool IsOk = false;
            try
            {
                //new Evan.DataBase.MsSql.DbHelper().BackDataBase();
                new Evan.DataBase.DbOperat().BackUpDataBase();
                IsOk = true;
            }
            catch(Exception ex)
            {
                IsOk = false;
                Evan.SystemLog.MemLog.AddExLog2("数据库备份出错", null, ex.ToString());
            }

            if (IsOk)
            {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_back&states=1");
            }
            else {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_back&states=-1");
            }
        }

#if false
        public void ReStore()
        {
            bool IsOk = false;
            try
            {
                string FilePath = Evan.Common.CommFun.GetQueryString("file");
                new Evan.DataBase.MsSql.DbHelper().ReStoreDataBase(FilePath);
                IsOk = true;
            }
            catch (Exception ex)
            {
                IsOk = false;
                Evan.SystemLog.MemLog.AddExLog2("数据库恢复出错", null, ex.ToString());
            }
            if (IsOk)
            {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_restore&states=1");
            }
            else {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_restore&states=-1");
            }
        }
#endif

        public void DownLoad()
        {
            try
            {
                string FilePath = "~/Add_Data/DbBackFolder/" + Evan.Common.CommFun.GetQueryString("file");
                Evan.Common.DirFile.DownloadFile(Request, Response, FilePath, 1024);
                //Response.Redirect(Request.Url.LocalPath + comurl + "&op=download&states=1");
            }
            catch
            {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_download&states=-1");
            }
        }
        public void Delete()
        {
            bool IsOk = false;
            try
            {
                string FilePath = "~/Add_Data/DbBackFolder/" + Evan.Common.CommFun.GetQueryString("file");
                Evan.Common.DirFile.DeleteFile(FilePath);

                if (File.Exists(Server.MapPath(FilePath)))
                {
                    File.Delete(Server.MapPath(FilePath));
                }

                IsOk = true;
            }
            catch (Exception ex)
            {
                IsOk = false;
                Evan.SystemLog.MemLog.AddExLog2("删除数据库备份出错", null, ex.ToString());
            }
            if (IsOk)
            {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_delete&states=1");
            }
            else {
                Response.Redirect(Request.Url.LocalPath + comurl + "&op=db_delete&states=-1");
            }
        }
        public void GetBakList()
        {
            DbBackList = GetAllPath("/App_Data/DbBackFolder/", "*.bak");
            DbBackList.AddRange(GetAllPath("/App_Data/DbBackFolder/", "*.sql"));
        }

        /// <summary>
        /// 获取目录下面的文件
        /// </summary>
        /// <param name="DirPath">目录名</param>
        /// <param name="SearchPattern">搜索 如 *.txt</param>
        /// <param name="IsDir">是否搜索目录</param>
        /// <returns></returns>
        private List<Evan.Model.SysModel.FileModel> GetAllPath(string DirPath, string SearchPattern)
        {

            DirectoryInfo dir = new DirectoryInfo(Server.MapPath("~" + DirPath));

            List<Evan.Model.SysModel.FileModel> filelist = new List<Evan.Model.SysModel.FileModel>();

            if (string.IsNullOrEmpty(SearchPattern))
            {
                foreach (FileInfo file in dir.GetFiles("*")) //设置文件类型  
                {
                    filelist.Add(
                        new Evan.Model.SysModel.FileModel { Name = file.Name, FullPath = file.FullName, CurPath = (file.DirectoryName.Replace(HttpContext.Current.Server.MapPath("/").TrimEnd('\\'), "")), Type = "File", Suffix = file.Extension, Size = file.Length, AddTime = file.CreationTime, EditTime = file.LastWriteTime }
                        );
                }
            }
            else
            {
                foreach (FileInfo file in dir.GetFiles(SearchPattern, SearchOption.AllDirectories)) //设置文件类型  
                {
                    filelist.Add(
                        new Evan.Model.SysModel.FileModel { Name = file.Name, FullPath = file.FullName, CurPath = (file.DirectoryName.Replace(HttpContext.Current.Server.MapPath("/").TrimEnd('\\'), "")), Type = "File", Suffix = file.Extension, Size = file.Length, AddTime = file.CreationTime, EditTime = file.LastWriteTime }
                        );
                }
            }
            return filelist;
        }

    }
}